package com.itheima;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

public class ArrayListDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("张三");
        list.add(0, "list");
        String s = list.get(1);
        System.out.println(s);

        list.set(0, "王五");
        list.remove(1);

        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        System.out.println(list);
        System.out.println("---------------------集合遍历方法1---------------------");
        for (int i = 0; i < list.size(); i++) {
            String obj = list.get(i);
            System.out.println(obj);
        }
        System.out.println("---------------------集合遍历方法2---------------------");
        for (String obj : list) {
            System.out.println(obj);
        }
        System.out.println("---------------------集合遍历方法3---------------------");
        Iterator<String> iterator = list.iterator();
/*        String next = iterator.next();
        boolean hasNext = iterator.hasNext();
        System.out.println(next);
        System.out.println(hasNext);*/

        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        System.out.println("---------------------集合遍历方法3---------------------");
        //lambda表达式本就是一个函数
       /* Consumer<String> consumer = str-> System.out.println(str);
        list.forEach(consumer);*/
        /*    list.forEach(str-> System.out.println(str));*/
/*        list.forEach(System.out::println);
    }*/
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        list.forEach(name -> System.out.println(name));
    }
}
//ArrayList扩容机制是：将旧数组的容量1.5倍，创建新的数组，将旧数组的元素复制到新数组中，旧数组被丢弃，